﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Viewer.BusinessObject.BusinessEntities;
using Ext.Net;

namespace Viewer.Code.UserControls
{
    public partial class AddCommonCode : System.Web.UI.UserControl
    {
        private List<CHCommonCode> lstCommonCode;
        CommonCode common = new CommonCode();
        CHCommonCode item = new CHCommonCode();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private void ResetControl()
        {

            txtCodeString.Text = string.Empty;
            txtGhiChu.Text = string.Empty;
            txtCodeValue.Text = string.Empty;
            cbbCodeIdType.SelectedItem.Value = string.Empty;
            cboCodeIDCha.SelectedItem.Value = string.Empty;

        }
        public void ShowPopUp_DetailCommonCode(string type, string codeId, string loaiMa)
        {
            hdLoaiMa.Text = loaiMa;
            hdCodeId.Text = codeId;
            hdType.Text = type;

            txtCodeKey.Text = loaiMa;
            storeCodeCha.DataSource = common.LoadDataCommonByCodeKey(loaiMa);
            storeCodeCha.DataBind();
            storeLoaiMa.DataSource = common.LoadDataCommonByCodeKey(loaiMa);
            storeLoaiMa.DataBind();

            if (type.Equals("Edit"))
            {
                wdAddCommonCode.Title = "Sửa mã";
                LoadDataControlByCodeId(Convert.ToInt32(codeId));
            }
            else if (type.Equals("View"))
            {

                cbbCodeIdType.ReadOnly = true;
                cboCodeIDCha.ReadOnly = true;
                btnSave.Hidden = true;
                wdAddCommonCode.Title = "Xem mã";
                LoadDataControlByCodeId(Convert.ToInt32(codeId));
            }
            else if (type.Equals("AddNew"))
            {
                ResetControl();
                btnSave.Hidden = false;
                wdAddCommonCode.Title = "Tạo mới mã";

            }
            wdAddCommonCode.Show();
        }
        private void LoadDataControlByCodeId(int codeId)
        {
            lstCommonCode = (List<CHCommonCode>)Session["CHCommonCode"];
            CHCommonCode item = lstCommonCode.Where(a => a.CodeID.Equals(codeId)).SingleOrDefault();
            txtCodeKey.Text = item.CodeKey;
            txtCodeString.Text = item.CodeString;
            txtCodeValue.Text = item.CodeValue;
            txtGhiChu.Text = item.GhiChu;

            cboCodeIDCha.SelectedItem.Value = item.CodeIDCha > 0 ? item.CodeIDCha.ToString() : string.Empty;
            cbbCodeIdType.SelectedItem.Value = item.CodeIDType.HasValue && item.CodeIDType > 0 ? item.CodeIDType.Value.ToString() : string.Empty;

        }
       
        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            if (!CheckTonTai.Value.Equals("TonTai"))
            {
                int idCha = !string.IsNullOrEmpty(cboCodeIDCha.SelectedItem.Value) ? Convert.ToInt32(cboCodeIDCha.SelectedItem.Value) : 0;
                int codeIdType = !string.IsNullOrEmpty(cbbCodeIdType.SelectedItem.Value) ? Convert.ToInt32(cbbCodeIdType.SelectedItem.Value) : 0;
                if (hdType.Text.Equals("AddNew"))
                {

                    CHCommonCode itemNew = item.GetCommonCode(txtCodeKey.Text, txtCodeValue.Text, codeIdType, idCha, txtGhiChu.Text, txtCodeString.Text);
                    ((UCDuLieuDetail)this.Parent).GetCommonCode(itemNew);

                }
                else
                {
                    item.EditCommonCode(Convert.ToInt16(hdCodeId.Text), txtCodeKey.Text, txtCodeValue.Text, codeIdType, idCha, txtGhiChu.Text, txtCodeString.Text);
                    ((UCDuLieuDetail)this.Parent).ReloadGrid("CHCommonCode");

                }
                ((UCDuLieuDetail)this.Parent).BindGrid(txtCodeKey.Text);
                wdAddCommonCode.Hide();
                X.Mask.Hide();
            }
            X.Mask.Hide();
        }
    }
}